package com.nf.health.app.mine.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.CommonAdapter;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.models.HealthMoneyHistoryBean;
import com.nf.health.app.utils.DialogHelp;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康币
 *
 * @author changpeng
 */
public class HealthyCurrencyActivity extends BaseActivity {
    @Bind(R.id.titlebar_back_view)
    ImageView titlebarBackView;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    @Bind(R.id.tv_no_record)
    TextView tvNoRecord;
    @Bind(R.id.titlebar_right_tv)
    TextView titlebarRightTv;
    @Bind(R.id.tv_health_currency)
    TextView tvHealthCurrency;
    @Bind(R.id.health_currency_stream_list)
    ListView healthCurrencyStreamList;
    CommonAdapter<HealthMoneyHistoryBean.HealthMoneyInfo> mAdapter;
    List<HealthMoneyHistoryBean.HealthMoneyInfo> healthMoneyHistoryBeenList = new ArrayList<>();
    public static final String GET_HEALTH_HISTORY = "getHealthHistory";
    public static final String GET_USER_SCORE = "getUserScore";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_healthy_currency);
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }

    private void initData() {
        provider.getUserHealthMoenyList(GET_HEALTH_HISTORY);

    }

    private void initView() {
        titlebarRightView.setImageResource(R.drawable.inquiry_question);
        titlebarTitleTv.setText("健康币");
        String healthMoney = PreferenceHelper.getString(PreferenceHelper.HEALTH_MOENY,"0");
        tvHealthCurrency.setText( healthMoney);
    }

    private void initListener() {
        titlebarRightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogHelp.showH5Dialog(HealthyCurrencyActivity.this, Constants.H5_TIP_GET_HEALTH_MONEY);
            }
        });
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if(GET_HEALTH_HISTORY.equals(actionName)){
            HealthMoneyHistoryBean healthMoneyHistoryBean = (HealthMoneyHistoryBean) object;
            healthMoneyHistoryBeenList = healthMoneyHistoryBean.getData();
            if(healthMoneyHistoryBeenList.size() == 0){
                tvNoRecord.setVisibility(View.VISIBLE);
            }else{
                tvNoRecord.setVisibility(View.GONE);
            }
            healthCurrencyStreamList.setAdapter(mAdapter = new CommonAdapter<HealthMoneyHistoryBean.HealthMoneyInfo>(
                    getApplicationContext(), healthMoneyHistoryBeenList, R.layout.item_health_currency_stream)
            {
                @Override
                public void convert(ViewHolder helper, HealthMoneyHistoryBean.HealthMoneyInfo item) {
                    helper.setText(R.id.tv_action,item.getDesc());
                    helper.setText(R.id.tv_current,item.getMoney());
                    helper.setText(R.id.tv_time_history,Util.timeStamp2Date(item.getCreatetime(),"yyyy-MM-dd HH:mm:ss"));
                   /* helper.setText(R.id.tv_time,Util.timeStamp2Date(item.getCreatetime(),"HH:mm"));*/

                    String str = item.getMoney().substring(0,1);
                    TextView tv = helper.getView(R.id.tv_current);

                    if(str.equals("-")){
                        tv.setTextColor(Color.rgb(255,165,0));
                    }else{
                        tv.setTextColor(Color.rgb(0,127,254));
                    }
                }
            });
        }
    }
}
